package ch.pizzapp.kurier;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.rest.RestService;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import ch.pizzapp.kurier.client.InfoClient;
import ch.pizzapp.kurier.data.ResponceData;
import ch.pizzapp.kurier.dto.InfoDto;

/**
 * Created by shanuka on 12/9/13.
 */
@EFragment(R.layout.login_activity)
public class LoginFragment extends Fragment {
	// @Override
	// public void onBackPressed() {
	// // TODO Auto-generated method stub
	// super.onBackPressed();
	// overridePendingTransition(R.anim.open_main, R.anim.close_next);
	// }
	@AfterViews
	void iniUi() {
		System.out.println("LoginActiviy");
		getInfo();
	}

	@Bean
	InfoDto infoDto;
	@RestService
	InfoClient InfoClient;

	@Background
	void getInfo() {

		infoDto.setDate("2014-02-25");
		infoDto.setDay("2");
		infoDto.setType("11");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setInfor(InfoClient.getInfo(infoDto.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("Not connected ");
			}
		});
	}

	@UiThread
	void setInfor(final ResponceData responceData) {

		System.out.println(responceData.getResponse().getAddress1());

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	@Click({ R.id.loginButton })
	void onClick(View view) {
		switch (view.getId()) {
		case R.id.loginButton:
			openDialog();
			break;
		default:
			break;

		}
	}

	@Trace
	private void openDialog() {
		final Dialog dialog = new Dialog(getActivity());
		dialog.setTitle("login");
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.login_dialog);
		dialog.getWindow().setBackgroundDrawable(
				new ColorDrawable(android.graphics.Color.TRANSPARENT));
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		dialog.getWindow().setGravity(Gravity.BOTTOM);
		Button btnDismiss = (Button) dialog.getWindow().findViewById(
				R.id.cancelButton);

		btnDismiss.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();
	}
}